package com.luojun.service;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.luojun.dao.CommodityDao;
import com.luojun.entity.Car;
import com.luojun.entity.Commodity;
import com.luojun.util.HibernateUtil;

public class UCommodityService {
	Session session =null;
	Transaction tran = null;
	CommodityDao commoditydao = new CommodityDao();
	/**
	 * 上架商品
	 * @param commodity
	 */
	public void save(Commodity commodity){
		if(commodity!=null){
			session =HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			commodity.setStatus(2);
			commoditydao.save(commodity);
			tran.commit();
		}else{
			System.out.println("commodity为空");
		}
	}
	/**
	 * 删除商品
	 * @param commodityId
	 */
	public void del(int commodityId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		commoditydao.del(commodityId);
		tran.commit();
	}
	/**
	 * 更新商品
	 * @param commodity
	 */
	public void update(Commodity commodity){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		commodity.setStatus(2);
		commoditydao.update(commodity);
		tran.commit();
	}
	/**
	 * buy 商品
	 * @param commodity
	 */
	public void buy(Commodity commodity){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		commodity.setStatus(0);
		commoditydao.update(commodity);
		tran.commit();
	}
	/**
	 * 根据名字查商品
	 * @param name
	 * @return
	 */
	public List<Commodity> findByname(String name){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		 List<Commodity> cList = commoditydao.findByname(name);
		 tran.commit();
		return cList;
	}
	/**
	 * 遍历商品 
	 * @return
	 */
	public List<Commodity> findStatusAndUser(int useid , int status){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findStatusAndUser(useid , status);
		tran.commit();
		if(cList==null){
			System.out.println("null");
		}
		return cList;
	}
	
	public int findCountbyUseid(int status ,int userid){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		int count =  commoditydao.findCountbyUseid(status, userid);
		tran.commit();
		return count;
	}

	/**
	 * 分页遍历
	 * @param pageNum
	 * @param pageSize
	 * status = 1
	 * @return
	 */
	public List<Commodity> findByPage(int nowpageNum,int pageSize){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByPage(nowpageNum, pageSize , 1);
		tran.commit();
		return cList;	
	}
	
	/**
	 * 获取总记录数
	 * status = 1
	 * @return
	 */
	public int findCountByPage(){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		int count =  commoditydao.findCount(1);
		tran.commit();
		return count;
	}
	
	/**
	 * 分页遍历
	 * @param pageNum
	 * @param pageSize
	 * status = 1
	 * @return
	 */
	public List<Commodity> findByPageName(int nowpageNum,int pageSize,String name){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByPageName(nowpageNum, pageSize , 1,name);
		tran.commit();
		return cList;	
	}
	
	/**
	 * 获取总记录数
	 * status = 1
	 * @return
	 */
	public int findCountbyName(String name){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		int count =  commoditydao.findCountbyName(1,name);
		tran.commit();
		return count;
	}
	/**
	 * 根据商品id查找
	 * @param commodityId
	 * @return
	 */
	public Commodity findByCommodityId(int commodityId){
		session =HibernateUtil.getCurrentSession();
		tran = session.beginTransaction();
		List<Commodity> cList = commoditydao.findByCommodityId(commodityId);
		Commodity commodity = (Commodity)cList.get(0);
		tran.commit();
		return commodity;
	}
	
	public List<Commodity> findsell(List<Car> list){
		List<Commodity> cLists = new ArrayList<>(); 
		for (Car car : list) {
			session =HibernateUtil.getCurrentSession();
			tran = session.beginTransaction();
			System.out.println(car+"--------");
			List<Commodity> cList = commoditydao.findByCommodityId(car.getCommodity().getCommodityId());
			Commodity commodity = (Commodity)cList.get(0);
			System.out.println(commodity+"--------");
			cLists.add(commodity);
			tran.commit();
		}
		return cLists;
	}

}
